package com.niaobulashi.controller;

import com.niaobulashi.queue.Customer;
import com.niaobulashi.queue.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class QueueTestController {
    @Autowired
    RedisTemplate redisTemplate;

    private Customer customer;
    private Producer producer;

    @PostConstruct
    public void init() {
        new Thread(() -> {
            customer = new Customer();
            customer.init(redisTemplate);
            customer.start();
            producer = new Producer();
            producer.init(redisTemplate);
        }).start();
    }

    /**
     * https://www.cnblogs.com/shamo89/p/9873368.html
     * redis 消息队列测试
     *
     * @return
     */
    @GetMapping("queueTest")
    public String testQueue() {
        for (int i = 0; i < 20; i++) {
            producer.putMessage(UUID.randomUUID().toString());
            System.out.println("main : 已存储消息条数:" + producer.getCount());
        }
        return "ok";
    }
}
